package com.yuchen.ishop.web;

import com.yuchen.core.cache.J2CacheUtils;
import com.yuchen.ishop.Gobals;
import com.yuchen.ishop.vo.CookieUserInfo;
import com.yuchen.ishop.web.mytags.MyEL;
import com.yuchen.utils.CookieUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jack Q
 */
public class BaseAction {

    public Timestamp getTime() {
        return new Timestamp(System.currentTimeMillis());
    }

    public Map successJson(String msg) {
        return jsonMap(true, msg);
    }

    public Map successJson() {
        return successJson(null);
    }

    public Map errorJson(String msg) {
        return jsonMap(false, msg);
    }

    public Map errorJson() {
        return errorJson(null);
    }

    public Map<String, Object> convertParameterMap(Map<String, String[]> parameterMap) {
        if(parameterMap!=null&&parameterMap.size()>0){
            Map map = new HashMap(parameterMap.size());
            String val = "";
            for(Map.Entry<String,String[]> param : parameterMap.entrySet()){
                val = StringUtils.join(param.getValue(),",");
                if(StringUtils.isBlank(val))continue;
                map.put(param.getKey(), val);
            }
            return map;
        }
        return null;
    }


    public boolean doCRSF(HttpServletRequest request) {
        String serverToken = (String) request.getSession().getAttribute(Gobals.CRSF_ID);
        request.getSession().setAttribute(Gobals.CRSF_ID, null);//清除CRSF_ID session
        if (serverToken == null) {
            return true;
        }
        String clinetToken = request.getParameter(Gobals.CRSF_ID);
        if (clinetToken == null) {
            return true;
        }
        if (!serverToken.equals(clinetToken)) {
            return true;
        }
        return false;
    }

    public static CookieUserInfo cookie2UserInfo(HttpServletRequest request) {
        String cookieVal = null;
        try {
            cookieVal = CookieUtils.findCookie(Gobals.COOKIE_ID, request);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(cookieVal)) {
            Map map = MyEL.decodeIshopid(cookieVal);
            CookieUserInfo userInfo = new CookieUserInfo();
            userInfo.setId(NumberUtils.toLong((String) map.get("id")));
            userInfo.setRole((String) map.get("role"));
            userInfo.setAccount((String) map.get("account"));
            userInfo.setState((Boolean) map.get("state"));
            if (map.get("sellerId") != null) {
                userInfo.setSellerId((Long) map.get("sellerId"));
            }
            return userInfo;
        }
        return null;
    }

    private Map jsonMap(boolean success, String msg) {
        Map map = new HashMap(3);
        map.put("success", success);
        map.put("msg", msg);
        return map;
    }
}
